package com.teradata.dashboard.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.app.ActionBar;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.teradata.dashboard.R;
import com.teradata.dashboard.constant.DashboardConstant;
import com.teradata.dashboard.utils.DialogUtil;
import com.teradata.dashboard.utils.FileHelper;
import com.teradata.dashboard.utils.HttpUtil;

public class PreferrnceActivity extends BaseActivity implements
		OnSharedPreferenceChangeListener {
	SharedPreferences sp;
	SharedPreferences.Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		setContentView(R.layout.preferrnce);

		Intent intent = getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		String userName = bundle.getString("USERNAME");
		TextView user = (TextView) findViewById(R.id.dl_setting_user_name);
		user.setText(userName);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setDisplayShowHomeEnabled(false);
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		getActionBar().setTitle(R.string.setting);

		Typeface iconfont = Typeface.createFromAsset(getAssets(),
				"fonts/fontawesome-webfont.ttf");
		((TextView) findViewById(R.id.setting_user_info_i))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_auto_load_i))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_font_size_i))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_clear_cache_i))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_about_i)).setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_check_upgrade_i))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_suggest_i)).setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_user_info_i))
				.setTypeface(iconfont);

		((TextView) findViewById(R.id.setting_user_info_a))
				.setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_about_a)).setTypeface(iconfont);
		((TextView) findViewById(R.id.setting_check_upgrade_a))
				.setTypeface(iconfont);

		// 设置该页面所有点击事件
		// 清空缓存
		RelativeLayout clearCache = (RelativeLayout) findViewById(R.id.setting_clearCache_Layout);
		clearCache.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
			}
		});

		// 关于我们
		RelativeLayout about_it = (RelativeLayout) findViewById(R.id.about_it);
		about_it.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				DialogUtil.showDialog(PreferrnceActivity.this, "teradata.com", false);
			}
		});
		
		// 检查更新
		RelativeLayout check_upgrade = (RelativeLayout) findViewById(R.id.check_upgrade);
		check_upgrade.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					JSONObject jsonVersion = getNewVersion();
					String newVersion = jsonVersion.getString("version");
					
					if(newVersion.equals(getVersionName()))
					{
						DialogUtil.showDialog(PreferrnceActivity.this, "你的版本是最新的", false);
					}
					else{
						DialogUtil.showDialog(PreferrnceActivity.this, "你的版本不是最新的", false);
					}
					
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
		
		
		// 发送建议
		RelativeLayout suggest = (RelativeLayout) findViewById(R.id.suggest);
		suggest.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
			}
		});
		
		

		sp = getSharedPreferences("com.teradata.dashboard", MODE_PRIVATE);
		editor = sp.edit();

		CheckBox autoLoadCK = ((CheckBox) findViewById(R.id.dl_setting_autoload_ck));
		autoLoadCK.setChecked(sp.getBoolean(DashboardConstant.AUTO_LOAD, true));
		autoLoadCK
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						editor.putBoolean(DashboardConstant.AUTO_LOAD,
								isChecked);
					}
				});

		final Spinner fontSizeSP = ((Spinner) findViewById(R.id.dl_setting_fonsize_spinner));
		fontSizeSP.setSelection(getSelection(sp.getString(
				DashboardConstant.FONT_SIZE,
				getResources().getText(R.string.setting_font_size_large)
						.toString())));
		fontSizeSP
				.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
						editor.putString(DashboardConstant.FONT_SIZE,
								fontSizeSP.getItemAtPosition(position)
										.toString());

					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub

					}
				});
		FileHelper.dropFile(this, DashboardConstant.FONT_SIZE_FILE, false);
		FileHelper
				.writeFile(this, DashboardConstant.FONT_SIZE_FILE, "1", false);
		FileHelper
				.writeFile(this, DashboardConstant.FONT_SIZE_FILE, "2", false);
		FileHelper
				.writeFile(this, DashboardConstant.FONT_SIZE_FILE, "3", false);

		List<String> fontSizrArr = new ArrayList<String>();
		fontSizrArr.addAll(FileHelper.readFile(this,
				DashboardConstant.FONT_SIZE_FILE, false));

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, fontSizrArr);

		fontSizeSP.setAdapter(adapter);
	}

	private int getSelection(String fontSize) {
		return 0;
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Toast.makeText(PreferrnceActivity.this, "" + key, Toast.LENGTH_SHORT)
				.show();
	}
	
	//向服务器发送请求
	// 定义发送请求的方法
	private JSONObject getUpdataInfo()
		throws Exception
	{
		//使用Map封装请求参数
		Map<String, String> map = new HashMap<String, String>();
		//map.put("userId", username);
		// 定义发送请求的URL
		String url = HttpUtil.BASE_URL + "";
		// 发送请求
		return new JSONObject(HttpUtil.postRequest(url, map));
	}
	
	private JSONObject getNewVersion()
		throws Exception
	{
		//使用Map封装请求参数
		Map<String, String> map = new HashMap<String, String>();
		//map.put("userId", username);
		// 定义发送请求的URL
		String url = HttpUtil.BASE_URL + "";
		// 发送请求
		return new JSONObject(HttpUtil.postRequest(url, map));
	}
	
	//获取当前版本号
	private String getVersionName() throws Exception{  
		//获取packagemanager的实例    
		PackageManager packageManager = getPackageManager();  
		//getPackageName()是你当前类的包名，0代表是获取版本信息   
		PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0);  
		return packInfo.versionName;
	}

}